package com.gxan.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxan.common.R;

import com.gxan.pojo.Resume;
import com.gxan.service.ResumeService;
import com.gxan.service.UserService;
import com.gxan.service.impl.ResumeServiceImpl;
import com.gxan.service.impl.UserServiceImpl;
import com.gxan.utils.DateUtils;
import com.gxan.utils.UploadFileUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/addResume")
public class AddResumeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



//        //获取前端传过来的参数
//        String job_title = request.getParameter("job_title");
//        String job_code = request.getParameter("job_code");
//        String job_classification = request.getParameter("job_classification");
//        String name = request.getParameter("name");
//        String sex = request.getParameter("sex");
//        String recruitment_type = request.getParameter("recruitment_type");
//        String email = request.getParameter("email");
//        String phone = request.getParameter("phone");
//        String registered_residence = request.getParameter("registered_residence");
//        String address = request.getParameter("address");
//        String political_landscape = request.getParameter("political_landscape");
//        String id_number = request.getParameter("id_number");
//        String graduate_school = request.getParameter("graduate_school");
//        String education_background = request.getParameter("education_background");
//        String major = request.getParameter("major");
//        String work_experience = request.getParameter("work_experience");
//        String salary_requirement = request.getParameter("salary_requirement");
//        String whether_in_office = request.getParameter("whether_in_office");
//        String fresh_graduate = request.getParameter("fresh_graduate");
//        String registration_time = request.getParameter("registration_time");
//        String personal_resume = request.getParameter("personal_resume");
//        //String resume_attachment = request.getParameter("resume_attachment");
//        String recommend_the_interview = request.getParameter("recommend_the_interview");
//        String referrer = request.getParameter("referrer");
//        String the_recommended_time = request.getParameter("the_recommended_time");
//        String recommended_interview_advice = request.getParameter("recommended_Interview_advice");
//
//       //封装对象
//        Resume resume = new Resume(null,job_title,job_code,
//                job_classification,name,sex,recruitment_type,email,phone,
//                registered_residence,address,political_landscape,id_number,
//                graduate_school,education_background,major,work_experience,
//                Integer.valueOf(salary_requirement),whether_in_office,fresh_graduate,
//                DateUtils.string2Date(registration_time),personal_resume,null,recommend_the_interview,
//                referrer, DateUtils.string2Date(the_recommended_time),recommended_interview_advice
//                );
//        //传递service处理数据
//        System.out.println(resume);
//        ResumeService addResumeService =new ResumeServiceImpl();
//        R r = addResumeService.AddResume(resume);
        //1.调用工具类的文件上传方法
        boolean result = UploadFileUtil.upload(request);
        R r =new R();
        if(result){//上传成功
            //获取表单数据，封装到了Map集合中
            Map<String, Object> map = UploadFileUtil.formData;
            map.put("registration_time",DateUtils.string2Date((String) map.get("registration_time")));
            map.put("the_recommended_time",DateUtils.string2Date((String) map.get("the_recommended_time")));
            //传递service处理数据
            Resume resume = new Resume();
            try {
                BeanUtils.populate(resume,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            System.out.println(resume);
            ResumeService addResumeService =new ResumeServiceImpl();
            r = addResumeService.AddResume(resume);


        }else{//上传失败
            r.setCode(500);
            r.setMsg("添加失败，请联系管理员");
        }

        //响应到前端页面
        //返回给前端需要的R封装的数据，装换成Json方式返回
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
